package com.sincetimes.game.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.io.ClassPathResource;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class ConfigManageView extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		String key=req.getParameter("key");
		String value=req.getParameter("value");
		String file=req.getParameter("file");
		String loginkey=WebUtil.getMD5Code("adminuserdragonstat");
		Cookie coo[]=req.getCookies();
		if(coo==null){
			resp.sendRedirect(ConfigManager.getInstance().getConfig("resurl")+"/login.dragon");
		}else{
			for(int i=0;i<coo.length;i++){
				Cookie cook=coo[i];
				if("loginkey".equals(cook.getName())){
					if(!loginkey.equals(cook.getValue())){
						resp.sendRedirect(ConfigManager.getInstance().getConfig("resurl")+"/login.dragon");
					}else{
						if(key!=null&&value!=null&&file!=null&&!"".equals(key)&&!"".equals(value)&&!"".equals(file)){
							writeData(key,value,file);
							req.setAttribute("result","result:"+key+"=="+ConfigManager.getInstance().getConfig(key));
						}
						Map map=ConfigManager.getInstance().getAllConfig();
						Iterator keyite=map.keySet().iterator();
						StringBuffer strbuf=new StringBuffer();
						while(keyite.hasNext()){
							String keystr=(String)keyite.next();
							strbuf.append(keystr).append("=").append(map.get(keystr)).append("</br>");
						}
						req.setAttribute("list",strbuf.toString());
						return new ModelAndView("upfile");
					}
				}else if(i==coo.length-1&&!"loginkey".equals(cook.getName())){
					resp.sendRedirect(ConfigManager.getInstance().getConfig("resurl")+"/login.dragon");
				}
			}
		}
		
		return null;
		
	}
	
	
	   
	    private static void writeData(String key, String value,String fileURL) {
	    	ClassPathResource resource = new ClassPathResource(fileURL);
	        Properties prop = new Properties();
	       
	        OutputStream fos = null;
	        try {
	        	InputStream is=resource.getInputStream();
	        	prop.load(is);
	        	is.close();//一定要在修改值之前关闭fis
	            fos = new FileOutputStream(resource.getFile());
	            prop.setProperty(key, value);
	            prop.store(fos, "Update '" + key + "', value="+value);
	            fos.close();
	            ConfigManager.getInstance().setConfig(key, value);
	           // ConfigManager.getInstance().loadConfig();
	            
	            
	        } catch (IOException e) {
	            System.err.println("Visit " + fileURL + " for updating " + value + " value error");
	           // e.printStackTrace();
	        }
	        finally{
	            try {
	                fos.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	    } 
	    
	    public static void main(String[] args) {
	        //ConfigUtil.getFileIO("name", "gxyTest.properties");
	        
	    	ConfigManageView.writeData("version", "110", "game.properties");
	        //System.out.println(ConfigUtil.getFileIO("name", "gxyTest.properties"));
	    }



}
